<template>
  <div id="roomPic">
    <ImgFiles :id="hotelId" :getPicListFn="getPicList" 
    :photoList="photoList" 
    :deleatItemFn="deleatItem" 
    :addupPicFn="addupPic" 
    routeUrl="/index/uploadRoomPage"/>
    <div class="information-btn">
      <el-form-item>
        <el-button  type="primary" @click="back">保存</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </div>
  </div>
</template>
<script>
import { updatePicture } from '@/public/js/hotelInfomation'
import {getRoomPicByid,addupRoomlPic,deleteRoomPictureList} from '@/public/js/room.js'
import ImgFiles from '@/components/common/imgFiles.vue'
export default {
  components: {
    ImgFiles
  },
  data () {
    return {
      roomId:'',
      photoList:[]
    }
  },
  created () {
    sessionStorage.removeItem('last')
    this.roomId = sessionStorage.getItem('roomId')
  },
  methods: {
    // 获取图片夹列表
    getPicList(){
      getRoomPicByid({roomId: this.roomId }).then(res=>{
        this.photoList = res.data.photoList
      })
    },
    // 删除图片
     deleatItem (item,index) {
      if (item.photoId) {
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteRoomPictureList({id:item.photoId}).then((res)=>{
            if (!res.data.errCode) {
              this.photoList.splice(index, 1)
               this.$message({
                  type: 'success',
                  message: '删除成功!'
              });
            }
          })
          //sessionStorage.setItem('imgsData', JSON.stringify(this.imgFile))
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }else{
         this.photoList.splice(index, 1)
      }
    },
    addupPic(item){
       addupRoomlPic({
       "roomId": this.roomId,
       "photoId": item.photoId ? item.photoId : '',
       "photoName": item.photoName
      }).then(res=>{
        if(!res.data.errCode) {
            this.getPicList();
            this.newFile = { 
              photoName: ''
            }
        }
      })
    },
    back () {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss">
  #roomPic{
    margin-top:30px;
  }
</style>
